//****************************************************************************
// Copyright (c) 2011-2013,WebJet Business Division,CYOU

// http://www.genesis-3d.com.cn

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//*****************************************************************************
Shader "PostEffect_GlowBlur" {
	Parameters{
		Vector [_ScaleFactor] = "1.0,1.0,1.0,1.0"
		Float  [_UvOffsetToNeighborPixelX] = "0.0"
		Float  [_UvOffsetToNeighborPixelY] = "0.0"
	}
	
	RenderQueue "CUSTOMDRAW"
	
	

			CODEBEGIN

			float4 _ScaleFactor;
			float  _UvOffsetToNeighborPixelX;
			float  _UvOffsetToNeighborPixelY;

			struct VS_OUTPUT
			{
				float4 Position		: POSITION0;   // vertex position 
				float2 centerCoordTex	: TEXCOORD0;
			};

			struct PS_INPUT
			{
				float2 centerCoordTex	: TEXCOORD0;
			};
			
			VS_OUTPUT mainVs(in float3 vPos : POSITION,in float2 vTex : TEXCOORD0)
			{
				VS_OUTPUT Output;
				Output.Position.xyzw = float4(vPos, 1.0f);
				Output.centerCoordTex.xy = vTex.xy;

				return Output;
			}

			float4 mainPs(PS_INPUT o) : COLOR0
			{
				float2 TexCoord = o.centerCoordTex;
				float2 offsetUV = float2(_UvOffsetToNeighborPixelX,_UvOffsetToNeighborPixelY);
				float4 stepUV0 = float4(-11.251852, -9.28917217, -7.32958603, -5.37268591);
				float4 stepUV1 = float4(-3.4179101, -1.46455705, 1.46455705, 3.4179101);
				float4 stepUV2 = float4(5.37268591, 7.32958603, 9.28917217, 11.251852);

				float weigths[7] = {0.000533999992, 0.00373300002, 0.0180040002, 0.059928, 0.137740001,0.218676999,0.122764997};
				float4 color;
				
				color =  tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV0.x) * weigths[0];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV0.y) * weigths[1];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV0.z) * weigths[2];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV0.w) * weigths[3];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV1.x) * weigths[4];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV1.y) * weigths[5];
				color += tex2D(g_MainBuffer, TexCoord) * weigths[6];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV1.z) * weigths[5];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV1.w) * weigths[4];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV2.x) * weigths[3];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV2.y) * weigths[2];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV2.z) * weigths[1];
				color += tex2D(g_MainBuffer, TexCoord + offsetUV * stepUV2.w) * weigths[0];

				color.xyz *= _ScaleFactor.xyz;
				
				color.w = 1.0f;
				return color;
			}
			CODEEND
	Technique{
		Pass{
		    Compiler
			          {
				       VertexShader = mainVs
				       PixelShader = mainPs			
			          }

			RenderSTATE { 
							cullMode "Back"
							ZTest Off
							ZWrite Off
							blendMode Off
							alphatest Off
							samplerState "g_MainBuffer" [Clamp] [Linear]
				     }
		}
	}
}